#include "seq_stack.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#define N 100

/*
typedef struct seq_stack_t {
  void *data;
  size_t size;
  size_t nmemb;
  size_t capacity;
} Stack;
*/

int stack_init(Stack **st, size_t size, size_t capacity) {
  *st = malloc(sizeof(Stack));
  if (*st == NULL) {
    return -1;
  }
  (*st)->data = malloc(size * capacity);
  memset((*st)->data, 0, size * capacity);
  (*st)->size = size;
  (*st)->capacity = capacity;
  return 1;
}

int stack_empty(const Stack *st) { return st->nmemb == 0; }

int stack_full(const Stack *st) { return st->nmemb == st->capacity; }

size_t stack_nmemb(const Stack *st) { return st->nmemb; }

void stack_pop(Stack *st) {
  if (stack_empty(st)) {
    return;
  }
  memset((char *)st->data + (st->nmemb - 1) * (st->size), 0, st->size);
  st->nmemb--;
}

void *stack_top(const Stack *st) {
  if (stack_empty(st)) {
    return NULL;
  }
  return (char *)st->data + (st->nmemb - 1) * (st->size);
}

int stack_push(Stack *st, const void *data) {
  if (stack_full(st)) {
    return -1;
  }
  memcpy((char *)st->data + (st->nmemb) * (st->size), data, st->size);
  st->nmemb++;
  return 1;
}

void stack_destory(Stack **st) {
  free((*st)->data);
  (*st)->data = NULL;
  free(*st);
  *st = NULL;
}
